/*
 * $Id: SparkController.java 32 2011-05-24 02:11:18Z rob.whelan@patientsknowbest.com $
 */
package com.representqueens.spark.service;

/*
 * 
 * Copyright 2007 Larry Ogrodnek
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.representqueens.spark.BarGraph;
import com.representqueens.spark.DataPoint;
import com.representqueens.spark.LineGraph;
import com.representqueens.spark.SizeParams;
import com.representqueens.spark.util.StringConversion;
import com.representqueens.util.StringUtils;

/**
 * @author Larry Ogrodnek <larry@cheesesteak.net>
 * @version $Revision: 32 $ $Date: 2011-05-24 02:11:18 +0000 (Tue, 24 May 2011) $
 */
public final class SparkController extends HttpServlet {
    @Override
    public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
		final List<DataPoint> data = StringConversion.getData(request.getParameter("data"));
	
		final String type = request.getParameter("type");
	
		final BufferedImage image;
	
		if (type != null && type.equals("line")) {
		    image = LineGraph.createGraph(data,
			    getSizeParams(request, LineGraph.DEFAULT_SIZE),
			    StringConversion.convertColor(request.getParameter("color"), LineGraph.DEFAULT_COLOR),
			    StringConversion.convertColor(request.getParameter("background"), null),
			    StringConversion.convertColor(request.getParameter("rangeColor"), LineGraph.DEFAULT_RANGE_COLOR));
		}
		else {
		    image = BarGraph.createGraph(data,
			    getSizeParams(request, BarGraph.DEFAULT_SIZE),
			    StringConversion.convertColor(request.getParameter("color"), BarGraph.DEFAULT_COLOR),
			    StringConversion.convertColor(request.getParameter("background"), null));
		}
	
		final String _output = request.getParameter("output");
	
		final String output = StringUtils.isEmpty(_output) ? "png" : _output;
	
		response.setContentType("image/" + output);
		ImageIO.write(image, output, response.getOutputStream());
		response.getOutputStream().close();
    }

    @Override
    public void doPost(final HttpServletRequest request,
	    final HttpServletResponse response) throws IOException {
    	doGet(request, response);
    }

    private static SizeParams getSizeParams(final HttpServletRequest request, final SizeParams defaults) {
		return new SizeParams(
			StringConversion.convertInt(request.getParameter("width"), defaults.getWidth()),
			StringConversion.convertInt(request.getParameter("height"), defaults.getHeight()),
			StringConversion.convertInt(request.getParameter("spacing"), defaults.getSpacing()),
			StringConversion.convertDouble(request.getParameter("forceYMin"), null),
			StringConversion.convertDouble(request.getParameter("forceYMax"), null),
			StringConversion.convertDouble(request.getParameter("forceXMin"), null),
			StringConversion.convertDouble(request.getParameter("forceXMax"), null));
    }
}
